package com.lib76.buildCode.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;

import com.lib76.buildCode.model.BuildCodeResult;
import com.lib76.buildCode.model.Project;
import com.lib76.buildCode.model.vm.ClassFieldData;

/**
 * 项目代码构建跟踪
 * @author 张浩
 * @version 0.0.2
 */
public class ProjectCodeBuildFuture {
	
	private Project project;
	private String projectFileRealPath;
	private String projectZipFilePath; //项目代码压缩文件的相对路径
	private Boolean success = true;
	
	private String mainPackageRealPath; //项目主代码包绝对路径
	private String mainResurcesRealPath; //项目主资源文件绝对路径
	private String mainWebRootRealPath; //WEB项目目录绝对路径
	private String testPackageRealPath; //项目测试代码包绝对路径

	private String currentRealPath; //当前根目录
	
	private List<ClassFieldData> constants; //全局变量
	private Map<String,IDelayBuilder<?>> delayBuilders; //延迟代码构建器
	private ArrayBlockingQueue<BuildCodeResult> results;
	
	public ProjectCodeBuildFuture(Project project){
		this.project = project;
		this.results = new ArrayBlockingQueue<BuildCodeResult>(100);
		this.constants = new ArrayList<ClassFieldData>();
		this.delayBuilders = new HashMap<String,IDelayBuilder<?>>();
	}
	
	public Project getProject() {
		return project;
	}
	public void setProject(Project project) {
		this.project = project;
	}
	public String getProjectFileRealPath() {
		return projectFileRealPath;
	}
	public void setProjectFileRealPath(String projectFileRealPath) {
		this.projectFileRealPath = projectFileRealPath;
	}
	public ArrayBlockingQueue<BuildCodeResult> getResults() {
		return results;
	}
	public void setResults(ArrayBlockingQueue<BuildCodeResult> results) {
		this.results = results;
	}
	public Boolean getSuccess() {
		return success;
	}
	public void setSuccess(Boolean success) {
		this.success = success;
	}
	public String getProjectZipFilePath() {
		return projectZipFilePath;
	}
	public void setProjectZipFilePath(String projectZipFilePath) {
		this.projectZipFilePath = projectZipFilePath;
	}
	public String getMainPackageRealPath() {
		return mainPackageRealPath;
	}
	public void setMainPackageRealPath(String mainPackageRealPath) {
		this.mainPackageRealPath = mainPackageRealPath;
	}
	public String getTestPackageRealPath() {
		return testPackageRealPath;
	}
	public void setTestPackageRealPath(String testPackageRealPath) {
		this.testPackageRealPath = testPackageRealPath;
	}
	public List<ClassFieldData> getConstants() {
		return constants;
	}
	public void setConstants(List<ClassFieldData> constants) {
		this.constants = constants;
	}
	public String getMainResurcesRealPath() {
		return mainResurcesRealPath;
	}
	public void setMainResurcesRealPath(String mainResurcesRealPath) {
		this.mainResurcesRealPath = mainResurcesRealPath;
	}
	public String getMainWebRootRealPath() {
		return mainWebRootRealPath;
	}
	public void setMainWebRootRealPath(String mainWebRootRealPath) {
		this.mainWebRootRealPath = mainWebRootRealPath;
	}
	public String getCurrentRealPath() {
		return currentRealPath;
	}
	public void setCurrentRealPath(String currentRealPath) {
		this.currentRealPath = currentRealPath;
	}
	public Map<String, IDelayBuilder<?>> getDelayBuilders() {
		return delayBuilders;
	}
	public void setDelayBuilders(Map<String, IDelayBuilder<?>> delayBuilders) {
		this.delayBuilders = delayBuilders;
	}
	
	/**
	 * 保存一个延迟代码构建器
	 * @param builder
	 */
	public <T extends IDelayBuilder<?>> T addDelayBuilder(T builder){
		delayBuilders.put(builder.getName(), builder);
		return builder;
	}
	
	/**
	 * 获取一个指定的延迟代码构建器
	 * @param className
	 * @param key
	 * @return
	 */
	public<T extends IDelayBuilder<?>> T getDelayBuilder(Class<T> className,String key){
		IDelayBuilder<?> builder = delayBuilders.get(key);
		if(builder != null){
			return (T)builder;
		}
		return null;
	}

	public void put(BuildCodeResult result){
		if(result.getSuccess() == false){
			this.success = false;
		}
		try {
			this.results.put(result);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public String getRealPath(String path){
		return currentRealPath+path;
	}
	
}
